package com.example.yls.demoa;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MainActivity extends AppCompatActivity {

    private MyView wiziqiView;
    private  String matchId="123456";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wiziqiView = findViewById(R.id.id_wiziqi);
        //启动一个定时器，每隔50毫秒查询一下该盘对阵
        startTimeForQuery();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.munu_main,menu);
        return true;

    }

    private void startTimeForQuery() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                BmobQuery<WuziData> wuziDataBmobQuery = new BmobQuery<>();

                wuziDataBmobQuery.addWhereEqualTo("matchId",matchId);
                wuziDataBmobQuery.findObjects(new FindListener<WuziData>() {
                    @Override
                    public void done(List<WuziData> list, BmobException e) {
                   if (e==null && list !=null && list.size()>0){
                       WuziData wuziData = list.get(0);
                       wiziqiView.setData(wuziData);
                   }
                    }
                });
            }
        }, 500,500);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id==R.id.action_settings){
            wiziqiView.start();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
